import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Rectangle;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

public class InsertDataPacienteFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JTextField textNome = null;
	private JTextField textIdade = null;
	private JLabel labelNome = null;
	private JLabel labelIdade = null;
	private JButton botaoInsereNoBanco = null;
	
	
	private Connection connection;  //  @jve:decl-index=0:
	private JTextArea textAreaMessage = null;
	private JScrollPane jScrollPane = null;
	private JButton botaoGetData = null;
	
	
	private GetDataPaciente getData;
	
	
	/**
	 * This is the default constructor
	 */
	public InsertDataPacienteFrame() {
		super();
		
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			System.out.println("ok!");
			} 
		catch (Exception e) {
			System.err.println("Unable to find and load driver");
			System.exit(1);
		}
		
		initialize();
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(249, 212);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
		
		connectToDB();
		
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			labelIdade = new JLabel();
			labelIdade.setBounds(new Rectangle(27, 47, 38, 16));
			labelIdade.setText("Idade:");
			labelNome = new JLabel();
			labelNome.setBounds(new Rectangle(27, 22, 38, 16));
			labelNome.setText("Nome:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBackground(new Color(224, 183, 164));
			jContentPane.add(getText1(), null);
			jContentPane.add(getTextIdade(), null);
			jContentPane.add(labelNome, null);
			jContentPane.add(labelIdade, null);
			jContentPane.add(getBotaoInsereNoBanco(), null);
			jContentPane.add(getTextAreaMessage(), null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getBotaoGetData(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes text1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getText1() {
		if (textNome == null) {
			textNome = new JTextField();
			textNome.setBounds(new Rectangle(71, 19, 130, 20));
		}
		return textNome;
	}

	/**
	 * This method initializes textIdade	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTextIdade() {
		if (textIdade == null) {
			textIdade = new JTextField();
			textIdade.setBounds(new Rectangle(71, 49, 130, 20));
		}
		return textIdade;
	}

	/**
	 * This method initializes botaoInsereNoBanco	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBotaoInsereNoBanco() {
		if (botaoInsereNoBanco == null) {
			botaoInsereNoBanco = new JButton();
			botaoInsereNoBanco.setBounds(new Rectangle(17, 80, 96, 26));
			botaoInsereNoBanco.setText("Insert Data");
			botaoInsereNoBanco.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					saveToDB();
				}
			});
		}
		return botaoInsereNoBanco;
	}

///////////////////
	
	public void connectToDB() {
		//connects to bcmed db! :)
		try {
			connection = DriverManager.getConnection(
			"jdbc:mysql://localhost/bcmed?user=root&password=bentes123");
		} catch(SQLException e) {
			System.out.println("Unable to connect to database");
			System.exit(1);
		}
	}
	
	private void displaySQLErrors(SQLException e) {
		textAreaMessage.append("SQLException: " + e.getMessage() + "\n");
		textAreaMessage.append("SQLState: " + e.getSQLState()+ "\n");
		textAreaMessage.append("VendorError: " + e.getErrorCode()+ "\n");
	}
	
	
   

	private void saveToDB(){


	          try {
	            Statement statement = connection.createStatement();
	            int i = statement.executeUpdate("INSERT INTO paciente (nome, idade) " +
	            		"VALUES('" + textNome.getText() + "', " +
	            		Integer.parseInt(textIdade.getText()) + 
	            	")");

	            textAreaMessage.append("Inserted " + i + " rows successfully \n");
	            textAreaMessage.append("Nome: " + textNome.getText()+ "\n");
//	            accountNumberList.removeAll();
//	            loadAccounts();
	          } catch(SQLException insertException) {
	            displaySQLErrors(insertException);
	          }

	}

	/**
	 * This method initializes textAreaMessage	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getTextAreaMessage() {
		if (textAreaMessage == null) {
			textAreaMessage = new JTextArea();
			textAreaMessage.setBounds(new Rectangle(29, 116, 376, 48));
			textAreaMessage.setBackground(new Color(224, 183, 164));
			textAreaMessage.setFont(new Font("Dialog", Font.PLAIN, 10));
		}
		return textAreaMessage;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane(textAreaMessage);
			jScrollPane.setBounds(new Rectangle(29, 116, 175, 48));
		}
		return jScrollPane;
	}

	/**
	 * This method initializes botaoGetData	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBotaoGetData() {
		
		if (botaoGetData == null) {
			botaoGetData = new JButton();
			botaoGetData.setBounds(new Rectangle(118, 80, 96, 26));
			botaoGetData.setText("Get Data");
			botaoGetData.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getData = new GetDataPaciente();
					getData.setVisible(true);
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					
					
				}
			});
		}
		return botaoGetData;
	}



}  //  @jve:decl-index=0:visual-constraint="69,16"
